package com.sfh.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Base64;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBException;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public abstract class BaseController extends Controller {
	private static final boolean logging = true;

	private static final class TaskSigle {
		private static final TaskQueue TASK_QUEUE = new TaskQueue();
	}

	private static class TaskQueue implements ObservableOnSubscribe<Boolean>{
		private BlockingQueue<Task> mQueue = new LinkedBlockingQueue<Task>();
		private Disposable mDisposable;

		public TaskQueue() {
			this.init();
		}

		public void putTask(Task task) {
			this.mQueue.offer(task);
		}

		private void init() {
			if (this.mDisposable != null) {
				this.mDisposable.dispose();
			}
			this.mDisposable = Observable.create(this).subscribeOn(Schedulers.io()).subscribe(new Consumer<Boolean>() {

				@Override
				public void accept(Boolean arg0) throws Exception {
					System.out.println("任务处理结果：" + arg0);
				}
			});

		}
		@Override
		public void subscribe(ObservableEmitter<Boolean> arg0) throws Exception {

			while (true) {
				try {
					Task task = mQueue.take();
					boolean success = task.runTask();
					if (!success) {
						// 任務不成功，再次執行
						putTask(task);
					}
					arg0.onNext(success);
				
				} catch (Exception e) {
					System.out.println("任务处理结果E：" +  e);
				}
			}
		
		}
	}

	public void putTask(Task task) {
		TaskSigle.TASK_QUEUE.putTask(task);
	}

	@Override
	public void renderJson(Object object) {
		if (logging) {
			System.out.println("返回数据：" + object);
		}

		super.renderJson(object);
	}

	/**
	 * 获取请求数据
	 * 
	 * @param valueType
	 * @return
	 * @throws IOException
	 */
	protected <T> T getRequestObject(Class<T> valueType) throws IOException {
		StringBuilder json = this.getRequestStr();
		if (logging) {
			System.out.println("请求数据：" + json);
		}
		return JsonKit.parse(json.toString(), valueType);
	}

	/**
	 * 获取请求数据
	 * 
	 * @param valueType
	 * @return
	 * @throws IOException
	 * @throws JAXBException
	 */
	protected StringBuilder getRequestStr() throws IOException {

		HttpServletRequest request = this.getRequest();
		String method = request.getServletPath();

		System.out.println("method:" + method);
		StringBuilder json = new StringBuilder(8192);
		BufferedReader reader = request.getReader();
		String line = null;
		while ((line = reader.readLine()) != null) {
			json.append(line);
		}
		return json;
	}
	
	/***
	 * base64编码
	 * @param content
	 * @return
	 */
	public String getEncoder(String content) {
		return new String(Base64.getEncoder().encode(content.getBytes()));
	}
	
}
